//
//  searchViewController.m
//  Wedding fantastic
//
//  Created by dllo on 15/6/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "searchViewController.h"
#import "NetworkTool.h"
#import "searchModel.h"

#define kHeight [UIScreen mainScreen].bounds.size.height
#define kWidth [UIScreen mainScreen].bounds.size.width

@interface searchViewController ()<UISearchBarDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
// 题目标签
@property (nonatomic, retain)UILabel *titleLabel;
// 关键字集合视图
@property (nonatomic, retain)UICollectionView *keywordsCollectionView;
@end

@implementation searchViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.keywordArr = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc {
    [_keywordArr release];
    [_keywordsCollectionView release];
    [_titleLabel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self createData];
    
    
    [self createView];
}
- (void)createData {
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    [NetworkTool get:@"http://www.hunliji.com/v1/api/app/keywords/hot.json" body:nil httpResult:JSON success:^(id result) {
        //
        NSMutableDictionary *resultDic = result;
        NSMutableArray *keywordsArr = resultDic[@"keywords"];
        for (NSMutableDictionary *tempDic in keywordsArr) {
            searchModel *model = [[searchModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.keywordArr addObject:model];
        }
        [self.keywordsCollectionView reloadData];
        [hub hide:YES];
    } failure:^(NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"悲催" message:@"数据加载失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
    }];
}
- (void)createView {
    // 搜索栏
    UISearchBar *mySearch = [[UISearchBar alloc] initWithFrame:CGRectMake(20 * kWidth / 375, 5 * kHeight / 667, kWidth - 40 * kWidth / 375, 30 * kHeight / 667)];
    mySearch.delegate = self;
    mySearch.showsCancelButton = NO;
    mySearch.barStyle = UIBarStyleBlackOpaque;
    mySearch.placeholder = @"请输入关键字";
    mySearch.keyboardType = UIKeyboardTypeWebSearch;
    [self.view addSubview:mySearch];
    [mySearch release];
    
    self.navigationItem.titleView = mySearch;
    
    // 题目标签
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30 * kHeight / 667)];
    [self.view addSubview:self.titleLabel];
    self.titleLabel.text = @"搜索热词";
    self.titleLabel.textColor = [UIColor grayColor];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    
    // 关键字结合视图
    // 1.集合视图布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 2.设置每个item的大小
    flowLayout.itemSize = CGSizeMake((kWidth - 10 * kWidth / 375) / 3, 30 * kHeight / 667);
    // 3.section中cell的边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * kWidth / 375, 10 * kHeight / 667, 10 * kWidth / 375, 10 * kHeight / 667);
    // 4.设置滑动方向
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 5.结合视图
    self.keywordsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20 * kWidth / 375, 50 * kHeight / 667, kWidth - 40 * kWidth / 375, kHeight) collectionViewLayout:flowLayout];
    self.keywordsCollectionView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 6.设置代理, 设置数据源
    self.keywordsCollectionView.delegate = self;
    self.keywordsCollectionView.dataSource = self;
    // 7.把集合视图添加到父是视图上
    [self.view addSubview:self.keywordsCollectionView];
    // 8.内存管理
    [self.keywordsCollectionView release];
    // 9. 注册
    // 第一个参数:需要激活指定注册类型
    // 第二个参数: 重用标志
    [self.keywordsCollectionView registerClass:[searchCell class] forCellWithReuseIdentifier:@"resue"];
}
// cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.keywordArr.count;
}
// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 获取数据
    searchModel *model = self.keywordArr[indexPath.row];
    //
    searchCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"resue" forIndexPath:indexPath];
    // 传递数据
    cell.keyLabel.text = model.name;
    //
    return cell;
}
// 被选中的cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    labelRecommendModel *model = self.keywordArr[indexPath.row];
    labelRecommendDetailViewController *labelRecommendDVC =[[labelRecommendDetailViewController alloc] init];
    // 传值
    labelRecommendDVC.ID = model.id;
    labelRecommendDVC.index = indexPath.row;
    
    labelRecommendDVC.detaiTitle = model.name;
    
    [self.navigationController pushViewController:labelRecommendDVC animated:YES];
    [labelRecommendDVC release];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
